<template>


  <div class="app-container" style="margin-left: 200px">
    <el-container>
      <el-row class="tac">
        <el-col :span="24">
          <el-menu
            ref="menus"
            default-active="2"
            class="el-menu-vertical-demo">


            <el-menu-item index="1" @click="to('/ucenter/myinfo')">
              <i class="el-icon-location"></i>
              <span slot="title">个人编辑</span>
            </el-menu-item>

            <el-menu-item index="2" @click="to('/ucenter/editpass')" >
              <i class="el-icon-menu"></i>
              <span slot="title">修改密码</span>
            </el-menu-item>
            <el-menu-item index="3" @click="to('/ucenter/myorder')">

              <i class="el-icon-setting"></i>
              <span slot="title">我的订单</span>

            </el-menu-item>
            <el-menu-item index="4" @click="to('/ucenter/mycomment')">
              <i class="el-icon-document"></i>
              <span slot="title">我的评论</span>
            </el-menu-item>


          </el-menu>
        </el-col>
      </el-row>


      <el-main >
        <div style="align-items: center">
          <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="80px">
            <el-form-item label="原密码" prop="oldPass">
              <el-input type="password" v-model="ruleForm.oldPass"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPass">
              <el-input type="password" v-model="ruleForm.newPass"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="rePass">
              <el-input type="password" v-model="ruleForm.rePass"></el-input>
            </el-form-item>
          </el-form>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </div>
      </el-main>
    </el-container>

  </div>

</template>

<script>
import memberApi from '@/api/member'
export default {
  name: "ucenter",
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.newPass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        oldPass: '',
        newPass: '',
        rePass: '',
      },
      rules: {
        oldPass: [
          {required: true, message: '原密码必填', trigger: 'blur'},
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
        newPass: [
          {required: true, message: '新密码必填', trigger: 'blur'},
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
        rePass: [
          {required: true, message: '确认密码必填', trigger: 'blur'},
          { validator: validatePass2, trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
      },
    }
  },

  methods: {
    to(router){
      this.$router.push({
        path: router
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {

          memberApi.updatePass(this.ruleForm).then((res) => {
            this.$message({
              message: '修改成功',
              type:'success'
            });

          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }

}
</script>

<style scoped>

</style>
